package com.naxian.controller.person;

import com.naxian.common.GetMsgCode;
import com.naxian.mapper.MycollectionMapper;
import com.naxian.pojo.*;


import com.naxian.service.mycollection.MycollectionService;
import com.naxian.service.person.PersonService;
import com.naxian.service.recruit.RecruitService;
import com.naxian.util.JsonResult;
import com.naxian.util.Sha2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PersonalController {
    @Autowired
    private PersonService personService;
    @Autowired
    private RecruitService recruitService;
    @Autowired
    private MycollectionMapper mycollectionMapper;
    @Autowired
    private MycollectionService mycollectionService;
    @GetMapping("person/{personalId}")
    public Persons getById(@PathVariable("personalId") Integer personalId) {
        Persons person = (Persons) personService.getById(personalId);
        return person;
    }

    @RequestMapping("person/login")
    @ResponseBody
    public JsonResult loginByUserAndPwd(Long perTel, String password, HttpServletRequest request) {
        System.out.println("你输入的perTel=" + perTel);
        System.out.println("你输入的password=" + password);
        JsonResult jsonResult = personService.loginByTelno(perTel, password);

        Persons person = (Persons) jsonResult.getData();

//        if (person != null) {
//           request.getSession().setAttribute("loginPer",person);
//        }
            if (jsonResult.getCode() == 0) {  //3.用户名不存在
                System.out.println("sdafa");


                return jsonResult;
            } else {  //2. 用户名存在
                System.out.println("agwgergre");
                request.getSession().setAttribute("loginPerId",person.getPerId());
                request.getSession().setAttribute("loginPerName",person.getPerName());
                request.getSession().setAttribute("loginPerTel", person.getPerTel());
                request.getSession().setAttribute("loginPerson", person);
                System.out.println(person.getPerTel());
                return new JsonResult(1, "用户存在，可以跳转", jsonResult);

            }

        }

    /**
     * 判断是否已登录
     * @param request
     * @return
     */
    @PostMapping("login/index")
        @ResponseBody
        public JsonResult longinBackIndex(HttpServletRequest request){
          Persons persons = (Persons) request.getSession().getAttribute("loginPerson");
          System.out.println(persons);
        return new JsonResult(1,"已经登陆",persons);//登陆之后把用户的信息传过去

        }

///**/注册验证码请求
    @PostMapping("person/getRegistCode")
    @ResponseBody
    public JsonResult getRegistCode(Long perTel) {

        JsonResult jsonResult = personService.getByTelno(perTel);
        if (jsonResult.getCode() == 0) { //用户存在 提醒用户登录
            System.out.println("用户存在，不能注册");
            return jsonResult;
        } else {
            System.out.println("用户不存在，可以注册，发送验证码"); //用户不存在 可以注册 发送验证码
            //2. 发送验证码
            GetMsgCode getMsgCode = new GetMsgCode();
            String codeMsg = null;
            String perTel1 = String.valueOf(perTel);//把long类型的数据改成String类型的
            try {
                codeMsg = getMsgCode.execute(perTel1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new JsonResult(1, "用户不存在可以注册", codeMsg);
        }
    }

    /**
     * 注册方法
     * @param perTel
     * @param pwd
     * @return
     */
    @PostMapping("persons/regist")
    @ResponseBody
    public JsonResult regist(Long perTel, String pwd , HttpServletRequest request) {
        System.out.println(perTel);

        Persons persons = new Persons(perTel, pwd,"罗小黑");
        Boolean insert = personService.insert(persons);
        if (insert == true){
            request.getSession().setAttribute("registPerName", persons.getPerName());
            request.getSession().setAttribute("registPerTel", persons.getPerTel());
            request.getSession().setAttribute("registPerson", persons);
            System.out.println(persons.getPerTel());
            return new JsonResult(0, "注册成功", persons);
        }else{
            return new JsonResult(1, "注册失败", null);
        }
    }


    //职位收藏功能
    @RequestMapping("jobApplication/collection")
    @ResponseBody
    public JsonResult recruitCollection(HttpServletRequest request){
        Integer recId = Integer.valueOf(request.getParameter("recId"));
        System.out.println("收藏职位id"+recId);
        Integer personId = (Integer) request.getSession().getAttribute("loginPerId");
        System.out.println("个人收藏id"+personId);
        Integer mycollectionStatus = 1;//默认的收藏状态，1为未删除
        MycollectionExample example = new MycollectionExample();
        MycollectionExample.Criteria criteria = example.createCriteria();
        criteria.andRecIdEqualTo(recId);
        criteria.andPerIdEqualTo(personId);
        criteria.andMycollectionStatusEqualTo(1); //查询方法
        List<Mycollection> mycollectionList = mycollectionMapper.selectByExample(example);

        System.out.println("查到的此职位收藏数据"+mycollectionList);
        if(mycollectionList.size()==0) {
            Mycollection mycollection = new Mycollection(recId, personId, mycollectionStatus);
            boolean temp = mycollectionService.insert(mycollection);
            System.out.println("生成的收藏职位对象" + mycollection);
            if (temp) {
                System.out.println("收藏成功" + mycollection);
                return new JsonResult(1, "收藏成功", mycollection);
            }else {
                return new JsonResult(3,"收藏失败",null);
            }
        }else {
            System.out.println("收藏失败");
            return new JsonResult(0, "收藏失败", null);
        }
    }
    //通过用户id查询收藏职位信息
    @RequestMapping("jobApplication/personIdSelectMycollection")
    @ResponseBody
    public JsonResult selectByPerId(HttpServletRequest request){
        Integer perId = (Integer) request.getSession().getAttribute("loginPerId");
        MycollectionExample mycollectionExample = new MycollectionExample();
        MycollectionExample.Criteria criteria = mycollectionExample.createCriteria();
        criteria.andMycollectionStatusEqualTo(1);
        criteria.andPerIdEqualTo(perId);
        List<Mycollection> mycollectionList = mycollectionMapper.selectByExample(mycollectionExample);
        System.out.println("要渲染的职位收藏集合"+mycollectionList);
        List<Recruit> recruitList = new ArrayList<>();
        for (Mycollection mycollection:mycollectionList) {
            Integer recId = mycollection.getRecId();
            Recruit recruit = recruitService.getByRecruitId(recId);
            System.out.println("职位"+recruit);
            recruitList.add(recruit);

        }


        return new JsonResult(1,"数据传输成功",recruitList);
    }
    //通过用户id查询收藏职位信息
    @RequestMapping("jobApplication/personIdSelectCompany")
    @ResponseBody
    public JsonResult selectByPersonId(HttpServletRequest request){
        Integer perId = (Integer) request.getSession().getAttribute("loginPerId");
        MycollectionExample mycollectionExample = new MycollectionExample();
        MycollectionExample.Criteria criteria = mycollectionExample.createCriteria();
        criteria.andMycollectionStatusEqualTo(1);//保证收藏的职位没有删除
        criteria.andPerIdEqualTo(perId);
        List<Mycollection> mycollectionList = mycollectionMapper.selectByExample(mycollectionExample);
        System.out.println("要渲染的职位收藏集合"+mycollectionList);


        List<Company> companyList = new ArrayList<>();
        List<String> comNameList = new ArrayList<>();

        for (Mycollection mycollection:mycollectionList) {
            Integer recId = mycollection.getRecId();
            Recruit recruit = recruitService.getByRecruitId(recId);
            Company company = recruitService.getCompanyByRecruitId(recId);
            String comName = company.getCompanyName();
            System.out.println("收藏职位对应的公司"+company);
            comNameList.add(comName);
            companyList.add(company);
        }


        return new JsonResult(1,"数据传输成功",comNameList);
    }
    //删除收藏职位功能
    @RequestMapping("jobApplication/delectMycollection")
    @ResponseBody
    public JsonResult delectMycollection(HttpServletRequest request){
        Integer recId = Integer.valueOf(request.getParameter("recId"));
        System.out.println("删除的收藏对象招聘id"+recId);
        Mycollection mycollection = mycollectionService.getByRecId(recId);
        System.out.println("通过招聘id查询的收藏对象"+mycollection);
        mycollection.setMycollectionStatus(0);
        boolean row = mycollectionService.update(mycollection);
        if(row){
            return new JsonResult(1,"删除成功",1);
        }
        return  new JsonResult(0,"删除失败",0);

    }


}
